package game.UIComponent
{
	import flash.events.MouseEvent;
	import flash.geom.Point;
	
	import game.common.CustomBitmap;
	import game.managers.PopManage;
	import game.managers.StageProxy;

	/**
	 * 面板基类
	 */
	public class Panel extends UIBase
	{
		/**面板上方的图片*/
		private var _headIcon:CustomBitmap;
		/**是否彈出*/
		private var _isPop:Boolean = false;
		/**默认背景*/
		private var _bg:CustomBitmap;
		/**使用拖拽*/
		public var useDrag:Boolean = true;
		/**记录鼠标当前的位置(mouseX,mouseY)*/
		public var px:Number;
		public var py:Number;
		
		public function Panel()
		{
			
		}
		
		override protected function init():void
		{
			super.init();
			
			addEventListener(MouseEvent.MOUSE_DOWN, swapTop);
		//	addEventListener(MouseEvent.MOUSE_UP,onUpHandler);
		}
		
		private function swapTop(e:MouseEvent):void
		{
			PopManage.getInstance().swapTop(this);
		}
		
//		public function onUpHandler(e:MouseEvent):void{
//		//	UIManager.stopDrag();
//		}
		
		
		/**
		 * 设置关闭按钮的位置
		 * @param point
		 * */
		public function set close(point:Point):void {
			var btn:Button = new Button("","close1");
			btn.addEventListener(MouseEvent.CLICK,onClose);
			btn.x = point.x;
			btn.y = point.y - 9;
			addChild(btn);
		}
		
		
		/**
		 * 点击关闭
		 * @param e
		 *
		 */
		public function onClose(e:MouseEvent):void
		{
			this.isPop=false;
		}
		
		
		/**
		 *  是否弹出
		 * @param pop
		 */
		public function set isPop (pop:Boolean):void
		{
			if (pop) {
				_isPop=true;
				PopManage.getInstance().popUp(this);
				openPanel();
			}else{
				_isPop=false;
				closePanel();
				PopManage.getInstance().popBack(this);
			}
		}
		
		
		protected function openPanel():void{};
		protected function closePanel():void{};
		
		
		/**
		 * 设置标题 _ 图片
		 * @param txt
		 *
		 */
		public function setImageTitle(img:String):void
		{
			_headIcon = new CustomBitmap();		
			_headIcon.source=img;
			_headIcon.y=-50;
			_headIcon.x=(width - _headIcon.width) / 2 - 5;
			_headIcon.addEventListener(MouseEvent.MOUSE_DOWN,DragDown);
			addChild(_headIcon);
		}
		
		/**
		 * 鼠标点下(拖动区域)
		 * @param e
		 */
		protected function DragDown(e:MouseEvent):void
		{
			if (!this.stage || !useDrag) return;
			this.stage.addEventListener(MouseEvent.MOUSE_UP, DragUp);
			this.stage.addEventListener(MouseEvent.MOUSE_MOVE, onDraging);
			px=stage.mouseX;
			py=stage.mouseY;
		}
		
		/**
		 * 鼠标弹起(拖动区域)
		 * @param e
		 *
		 */
		protected function DragUp(e:MouseEvent):void
		{
			if(!stage)return;
			this.stage.removeEventListener(MouseEvent.MOUSE_UP, DragUp);
			this.stage.removeEventListener(MouseEvent.MOUSE_MOVE, onDraging);
			
		}
		
		protected function onDraging(e:MouseEvent):void
		{
			if(!stage)return;
			this.x+=stage.mouseX - px;
			this.y+=stage.mouseY - py;
			px=stage.mouseX;
			py=stage.mouseY;
		}
		
		override public function set x(tox:Number):void
		{
			var value:int=int(tox);
			var stageWidth:Number = StageProxy.stageWidth();
			var borderWidth:int=int(this.width / 2 - 80);
			value=value > stageWidth - borderWidth ? stageWidth - borderWidth : value;
			value=value < borderWidth - this.width + 30 ? borderWidth - this.width + 30 : value;
			super.x=value;
		}
		
		override public function set y(toy:Number):void
		{
			var stageHeight:Number = StageProxy.stageHeight();
			var value:Number = int(toy);
			if (value > stageHeight - 20)
				value = stageHeight - 20;
			if (value < 5)
				value = 5;
			super.y=value;
		}
		
		
		/**
		 * 默认设置窗体位置于舞台居中，如需设置于特定坐标需重写此函数
		 * */
		public function setCenter():void
		{
			this.x=(StageProxy.width - this.width) >> 1;
			this.y=(StageProxy.height - this.height) >> 1;
		}
		
		/**
		 * 设置背景图片
		 * @param name 图片链接类名字
		 * @param w 宽度
		 * @param h 高度
		 * */
		public function setBgImage(name:String,w:Number,h:Number):CustomBitmap
		{
			if (!name) 
			{
				throw new Error("panel 的背景图片名字不能为空");
				return;
			}
			_bg = new CustomBitmap();
			_bg.source = name;
			_bg.width = w;
			_bg.height = h;
			
			addChildAt(_bg,0);
			
			return _bg;
			
		}
	}
}